#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
from app import create_app, db
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from flask import session, render_template, request, url_for, redirect
from flask_login import current_user

app = create_app()  # 初始化app
manager = Manager(app)  # flask_script
migrate = Migrate(app, db)  # 用于迁移数据库


# 登陆判断
@app.after_request
def process_request(e):
    # 如果static不在路由中, 用户没有登陆并且 /auth/login不在url中， 那么就返回登陆页
    # 如果是认证的页面，应当全跳过
    if '/static' not in request.path and '/auth' not in request.path:
        if not current_user.is_authenticated and '/auth/login' not in request.path:
                return redirect(url_for('auther.loginindex'))
    # if '/auth/register' in request.path:
    #     return e
    # 如果static在路由, auth/login在url中 并且没有登陆也会直接返回资源, 不走上面的条件
    return e


# 错误页面
@app.errorhandler(404)
def page_404(err):
    return render_template('public/404.html'), 404


manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    # 启动的时候需要在控制台中 输入 python xx.py runserver -h地址  -p端口
    manager.run()
    # app.run()
